package com.capri.adapter;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.capri.R;
import com.capri.activity.OrderDetailActivity;
import com.capri.model.bean.OrderBean;

import java.util.ArrayList;
import java.util.List;


public class OrderListAdapter extends BaseAdapter {


    private Context c;
    private List<OrderBean> datas = new ArrayList<>();

    public OrderListAdapter(Context context) {
        this.c = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(this.c).inflate(R.layout.adapter_order_simple, null);
        }
        initViews(convertView,position);
        return convertView;
    }

    private void initViews(View convertView,int position) {
        TextView tv_service_time = convertView.findViewById(R.id.tv_service_time);
        TextView tv_order_status = convertView.findViewById(R.id.tv_order_status);
        ImageView imv_header = convertView.findViewById(R.id.imv_header);
        TextView tv_patient_name = convertView.findViewById(R.id.tv_patient_name);
        TextView tv_service_name = convertView.findViewById(R.id.tv_service_name);
        TextView tv_service_address = convertView.findViewById(R.id.tv_service_address);
        tv_service_time.setText("服务时间: "+datas.get(position).getServiceDate());
        Glide.with(c).load(datas.get(position).getOrderPic()).into(imv_header);
        tv_patient_name.setText("患者姓名: "+datas.get(position).getPatientName());
        tv_service_name.setText("服务项目: "+datas.get(position).getOrderName());
        tv_service_address.setText("服务地址: "+datas.get(position).getServiceAddress());
        String serviceStatus=datas.get(position).getServiceStatus();
        if ("all".equals(serviceStatus)) {
            serviceStatus="";//全部
        }
        else if("waitConfirm".equals(serviceStatus)){
            serviceStatus="待接单";//待接单
        }
        else if("waitStartOff".equals(serviceStatus)){
            serviceStatus="待出诊";//待出诊
        }
        else if("startOff".equals(serviceStatus)){
            serviceStatus="已出诊";//已出诊
        }
        else if("arrived".equals(serviceStatus)){
            serviceStatus="已到达";//已到达
        }
        else if("startService".equals(serviceStatus)){
            serviceStatus="服务中";//已到达
        }
        else if("serviceFinish".equals(serviceStatus)){
            serviceStatus="已结束";//已结束
        }
        tv_order_status.setText(serviceStatus);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent((Activity)c, OrderDetailActivity.class);
                intent.putExtra("key_id",datas.get(position).getOrderId());
                c.startActivity(intent);
            }
        });
    }

    public void setDatas(List<OrderBean> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }
}
